package com.block.datapush.service.extdata;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.datapush.model.ProtocolInfoEntity;
import com.block.datapush.service.IPushExtDataService;
import com.block.db.entity.*;
import com.block.db.mapper.DataMatchOrderItemMapper;
import com.block.db.mapper.MerchantThirdBaseInfoMapper;
import com.block.db.mapper.MerchantThirdProtocolInfoMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 使用臻享分期 下游双授权的文档的机构，获取协议地址的方法
 */
@Service
public class ZhenXiangPushExtDataServie implements IPushExtDataService {

    @Resource
    DataMatchOrderItemMapper dataMatchOrderItemMapper;
    @Resource
    MerchantThirdProtocolInfoMapper merchantThirdProtocolInfoMapper;
    @Resource
    MerchantThirdBaseInfoMapper merchantThirdBaseInfoMapper;

    @Override
    public List<ProtocolInfoEntity> getProtocolUrl(MerchantBaseInfo merchantBaseInfo, MemberSourceInfo memberSourceInfo, JSONObject extObject) {

        if (ObjectUtil.isEmpty(extObject)){
            return new ArrayList<>();
        }

        String orderNo = extObject.getString("orderNo");
        String orderItemNo = extObject.getString("orderItemNo");

        // 查询协议列表
        DataMatchOrderItem matchOrderItem = dataMatchOrderItemMapper.selectById(orderItemNo);

        MerchantThirdBaseInfo thirdBaseInfo = merchantThirdBaseInfoMapper.selectEntityByOrderNo(matchOrderItem.getPushId());

        List<MerchantThirdProtocolInfo> protocolList = merchantThirdProtocolInfoMapper.selectListByThirdId(thirdBaseInfo.getId());

        List<ProtocolInfoEntity> resultList = protocolList.stream().map(protocolInfo -> {

            ProtocolInfoEntity infoEntity = new ProtocolInfoEntity();
            infoEntity.setProtocolUrl(protocolInfo.getProtocolUrl());
            infoEntity.setProtocolName(protocolInfo.getProtocolName());

            return infoEntity;
        }).collect(Collectors.toList());

        return resultList;
    }
}
